Passed
Push — master ( 75c06e...4206c8 )
by Night
01:08
created

stringFuncs.splitLines   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 2
nop 2
dl 0
loc 26
rs 8.6666
c 0
b 0
f 0
1
2
3
var arrayFuncs = {
4
	joinLines: function(windows = true){
5
		var lines = this;
6
		return windows ? lines.join("\r\n") : lines.join("\n");
7
	},
8
	none:null
9
};
10
11
// register funcs
12
UB.registerFuncs(Array.prototype, arrayFuncs);
0 ignored issues
show
Bug introduced by
The variable UB seems to be never declared. If this is a global, consider adding a /** global: UB */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
13
14
15
16
17
var stringFuncs = {
18
	
19
	splitLines: function(trimAll = false, delBlanks = false){
20
		var text = this;
21
		var lines = text.split("\r\n").join("\n").split("\r").join("\n").split("\n");
22
		
23
		// trim all lines if wanted
24
		if (trimAll || delBlanks) {
25
			for (var l = 0, ll = lines.length;l<ll;l++){
26
				
27
				// trim
28
				var text = String(lines[l]).replace(Regex.Trim, "");
0 ignored issues
show
Bug introduced by
The variable Regex seems to be never declared. If this is a global, consider adding a /** global: Regex */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Comprehensibility Naming Best Practice introduced by
The variable text already seems to be declared on line 20. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
29
				
30
				// skip this line if blank
31
				if (delBlanks && text == "") {
32
					lines.splice(l, 1);
33
					l--;
1 ignored issue
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable l here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
34
					ll--;
35
					continue;
36
				}
37
				
38
				// save
39
				lines[l] = text;
40
			}
41
			lines.length = ll;
42
		}
43
		return lines;
44
	},
45
	
46
	
47
	normalizeNewlines: function(finalNewline = "\n"){
48
		var text = this;
49
		return text.split("\r\n").join("\n").split("\r").join(finalNewline);
50
	},
51
	lineCount: function(windowsNewline = false){
52
		var text = this;
53
		return S.CountOf(text, windowsNewline ? "\r\n" : "\n") + 1;
0 ignored issues
show
Bug introduced by
The variable S seems to be never declared. If this is a global, consider adding a /** global: S */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
54
	},
55
	
56
	trimAndDelBlanks: function(windowsNewline = true){
57
		var s = this;
58
		return s.splitLines(true, true).joinLines(windowsNewline);
59
	},
60
61
	/** convert to single line, taking care to add spaces between the last word & first word of next line */
62
	toSingleLine: function(sep = " "){
63
		var s = this;
64
		s = s.trim();
65
		if (S.IsMultiline(s, false)) {
0 ignored issues
show
Bug introduced by
The variable S seems to be never declared. If this is a global, consider adding a /** global: S */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
66
			return s.splitLines(true, true).join(sep);
67
		}
68
		return s;
69
	},
70
	
71
	/** given a string and a char index within it, calculates which line that char came on (0 based) */
72
	lineIndexOfChar: function(charIndex){
73
		var text = this;
74
		var lineNum = 0;
75
		for (var c = 0;c<charIndex;c++){
76
			var code = text.charCodeAt(c);
77
			if (code == charCodes.NewlineN) {
0 ignored issues
show
Bug introduced by
The variable charCodes seems to be never declared. If this is a global, consider adding a /** global: charCodes */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
78
				lineNum++;
79
			}
80
		}
81
		return lineNum;
82
	},
83
	
84
	/** given a string and a char index within it, calculates which character on the line that char came on (0 based) */
85
	lineCharOfChar: function(charIndex){
86
		var text = this;
87
		var lineStart = 0;
88
		for (var c = 0;c<charIndex;c++){
89
			var code = text.charCodeAt(c);
90
			if (code == charCodes.NewlineN) {
0 ignored issues
show
Bug introduced by
The variable charCodes seems to be never declared. If this is a global, consider adding a /** global: charCodes */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
91
				lineStart = c;
92
			}
93
		}
94
		return charIndex - lineStart;
95
	},
96
	
97
	none:null
98
};
99
100
// register funcs
101
UB.registerFuncs(String.prototype, stringFuncs);
102
103
// top level
104
UB.newlines = function(newlines = 1, windows = false){
105
	var text = this;
106
	
107
	// quick
108
	if (newlines <= 0) {
109
		return text;
110
	}
111
	
112
	// quick
113
	var char = windows?"\r\n":"\n";
114
	if (newlines == 1) {
1 ignored issue
show
Best Practice introduced by
Comparing newlines to 1 using the == operator is not safe. Consider using === instead.
Loading history...
115
		return text + char;
116
	}
117
	if (newlines == 2) {
118
		return text + char + char;
119
	}
120
	if (newlines == 3) {
121
		return text + char + char + char;
122
	}
123
	
124
	// slow
125
	return text + char.repeat(newlines).join("");
126
};
127